using FytSoa.Application.Crm.Param;
using FytSoa.Common.Enum;
using FytSoa.Domain.Crm;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Crm;

/// <summary>
/// 项目-阶段服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v7")]
public class CrmProjectStageService : IApplicationService 
{
    private readonly SugarRepository<CrmProjectStage> _thisRepository;
    private readonly CrmOperateLogService _operateLogService;
    public CrmProjectStageService(SugarRepository<CrmProjectStage> thisRepository
    ,CrmOperateLogService operateLogService)
    {
        _thisRepository = thisRepository;
        _operateLogService = operateLogService;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<List<CrmProjectStageDto>> GetPagesAsync(WhereParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(param.Id!=0,m=>m.ProjectId==param.Id)
            .Includes(m=>m.Employee)
            .OrderBy(m=>m.Id)
            .ToListAsync();
        return query.Adapt<List<CrmProjectStageDto>>();
    }
    
    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<CrmProjectStageDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<CrmProjectStageDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(CrmProjectStageDto model) =>
        await _thisRepository.InsertAsync(model.Adapt<CrmProjectStage>());

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(CrmProjectStageDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<CrmProjectStage>());

    /// <summary>
    /// 推送至下一个阶段
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task<int> ModifyNextAsync(CrmProjectStageNextParam model)
    {
        var upModel=await _thisRepository.GetFirstAsync(m => m.ProjectId == model.ProjectId && !m.Status, m => m.Id, OrderEnum.Asc);
        if (string.IsNullOrEmpty(upModel.Name))
        {
            var projectRepository = _thisRepository.ChangeRepository<SugarRepository<CrmProject>>();
            await projectRepository.UpdateAsync(m => new CrmProject()
            {
                Status = CrmProjectStatusEnum.Completed
            },m=>m.Id==model.ProjectId);
            //更改项目状态为已完成
            await _operateLogService.AddAsync(new CrmOperateLogDto()
            {
                OperateId = model.ProjectId,
                Type = (int)OaOperateEnum.Project,
                Remark = "更新项目状态为：已完成"
            });
            return 2;
        }
        await _thisRepository.UpdateAsync(m => new CrmProjectStage()
        {
            Status = true
        },m=>m.Id==upModel.Id);
        await _operateLogService.AddAsync(new CrmOperateLogDto()
        {
            OperateId = model.ProjectId,
            Type = (int)OaOperateEnum.Project,
            Remark = "项目推送到：【"+upModel.Name+"】阶段"
        });
        return 1;
    }

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
